//
//  ESTlenAccount.m
//  adium-tlen
//
//  Created by Krzysztof Godlewski on 22/05/2007.
//  Copyright 2007 __MyCompanyName__. All rights reserved.
//

#import <Adium/AIAccountControllerProtocol.h>
#import <Adium/AIInterfaceControllerProtocol.h>
#import <Adium/AIStatusControllerProtocol.h>
#import <Adium/AIContactControllerProtocol.h>
#import <Adium/AIChat.h>
#import <Adium/AIHTMLDecoder.h>
#import <Adium/AIListContact.h>
#import <Adium/AIStatus.h>

#import "ESTlenAccount.h"

//
// 99% of this stuff is shamelessly copied from other ES*Account.m files.
// Oh well...

@implementation ESTlenAccount

- (const char*)protocolPlugin
{
	return "prpl-tlen";
}

- (NSString *) host
{
	AILog(@"Account::host");
	return @"s1.tlen.pl";
}

/*!
 * @brief Should set aliases serverside?
 *
 * Jabber supports serverside aliases.
 */
- (BOOL)shouldSetAliasesServerside
{
	return YES;
}

/*!
 * @brief Supports offline messaging?
 *
 * Jabber supports offline messaging.
 */
- (BOOL)canSendOfflineMessageToContact:(AIListContact *)inContact
{
	return YES;
}

#if 0
- (AIListContact *)contactWithUID:(NSString *)sourceUID
{
	AIListContact	*contact;
	
	AILog(@"TlenAccount::contactWithUID %@", sourceUID);
	
	contact = [[adium contactController] existingContactWithService:service
															account:self
																UID:sourceUID];
	if (!contact) {		
		contact = [[adium contactController] contactWithService:[self _serviceForUID:sourceUID]
														account:self
															UID:sourceUID];
	}
	
	return contact;
}
#endif
#if 0
- (NSString *)connectionStringForStep:(int)step
{
	AILog(@"connectionStringForStep %i", step);
	switch (step) {
		case 0:
			//return AILocalizedString(@"Connecting",nil);
			return @"Connecting";
			break;
		case 1:
			//return AILocalizedString(@"Initializing Stream",nil);
			return @"Initializing stream";
			break;
		case 2:
			//return AILocalizedString(@"Reading data",nil);
			return @"Reading data";
			break;			
		case 3:
//			return AILocalizedString(@"Authenticating",nil);
			return @"Authenticating";
			break;
		case 5:
//			return AILocalizedString(@"Initializing Stream",nil);
			return @"Initializing stream";
			break;
		case 6:
//			return AILocalizedString(@"Authenticating",nil);
			return @"Authenticating";
			break;
	}
	return nil;
}
#endif


- (const char *)purpleStatusIDForStatus:(AIStatus *)statusState
							  arguments:(NSMutableDictionary *)arguments
{
	const char		*statusID = NULL;
	NSString		*statusName = [statusState statusName];
	NSString		*statusMessageString = [statusState statusMessageString];
//	NSNumber		*priority = nil;
	
	AILog(@"Tlen::purpleStatusIDForStatus %@", [statusState statusName]);
	
	if (!statusMessageString) statusMessageString = @"";
	
	switch ([statusState statusType]) {
		case AIAvailableStatusType:
		{
			AILog(@"AIAwayStatusType");
			if (([statusName isEqualToString:STATUS_NAME_FREE_FOR_CHAT]) ||
				([statusMessageString caseInsensitiveCompare:[[adium statusController] localizedDescriptionForCoreStatusName:STATUS_NAME_FREE_FOR_CHAT]] == NSOrderedSame))
				statusID = "chat";
			//priority = [self preferenceForKey:KEY_JABBER_PRIORITY_AVAILABLE group:GROUP_ACCOUNT_STATUS];
			break;
		}
			
		case AIAwayStatusType:
		{
			AILog(@"Away");
			if (([statusName isEqualToString:STATUS_NAME_DND]) ||
				([statusMessageString caseInsensitiveCompare:[[adium statusController] localizedDescriptionForCoreStatusName:STATUS_NAME_DND]] == NSOrderedSame))
				statusID = "dnd";
			else if (([statusName isEqualToString:STATUS_NAME_EXTENDED_AWAY]) ||
					 ([statusMessageString caseInsensitiveCompare:[[adium statusController] localizedDescriptionForCoreStatusName:STATUS_NAME_EXTENDED_AWAY]] == NSOrderedSame))
				statusID = "xa";
			//priority = [self preferenceForKey:KEY_JABBER_PRIORITY_AWAY group:GROUP_ACCOUNT_STATUS];
			break;
		}
			
		case AIInvisibleStatusType:
			AILog(@"Invisible :)");
			//priority = [self preferenceForKey:KEY_JABBER_PRIORITY_AWAY group:GROUP_ACCOUNT_STATUS];
			statusID = "invisible";
			//			statusID = "Invisible";
			break;
			
		case AIOfflineStatusType:
			AILog(@"offiline status");
			break;
				  
		default:
			AILog(@"Cos innego: %i", [statusState statusType]);
	}
	
	//Set our priority, which is actually set along with the status...Default is 0.
	//[arguments setObject:(priority ? priority : [NSNumber numberWithInt:0])
	//			  forKey:@"priority"];
		
	//If we didn't get a purple status ID, request one from super
	if (statusID == NULL) {
		AILog(@"Nie bylo status");
		statusID = [super purpleStatusIDForStatus:statusState arguments:arguments];
	}
	
	AILog(@"Zwracam statusID: %s", statusID);
	
	return statusID;
}

@end
